mui.init({
	swipeBack: false,
})

App.init();

var scanCodeWrapperVm = new Vue({
	el: '#scanCodeWrapper',
	data: {},
	type: 0,
	scanWrapper: {},
	screenWidth: 0,
	prevWebviewId: '',
	mounted: function() {
		if(window.plus) {
			this.plusReady();
		} else {
			document.addEventListener('plusready', this.plusReady, false);
		}

	},
	methods: {
		createScanView: function() {
			var that = this;
			var scanWrapper = new plus.barcode.Barcode('scanCodeWrapper', [plus.barcode.QR], {
				frameColor: '#1d82d2',
				scanbarColor: '#1d82d2'
			}); //创建
			scanWrapper.onmarked = this.onmarked;
			scanWrapper.start();
			that.scanWrapper = scanWrapper;

		},
		onmarked: function(type, result, file) {
			var that = this;
			switch(type) {
				case plus.barcode.QR:
					type = 'QR';
					break;
				case plus.barcode.EAN13:
					type = 'EAN13';
					break;
				case plus.barcode.EAN8:
					type = 'EAN8';
					break;
				default:
					type = type;
					break;
			}
			var res = new Object();
			res.result = result.replace(/\n/g, '').replace(/\"/g, '');
			res.type = type;
			mui.fire(plus.webview.getWebviewById(that.prevWebviewId), 'getScanCode', res);
			mui.back();
		},
		plusReady: function() {
			var that = this;
			var screenWidth = plus.screen.resolutionWidth;
			that.screenWidth = screenWidth;
			that.type = plus.webview.currentWebview().type;
			that.prevWebviewId = plus.webview.currentWebview().prevWebviewId;
			setTimeout(function() {
				that.createScanView();
			}, 250);
			var titleView = new plus.nativeObj.View('NvTitle', {
				top: '0px',
				left: '0px',
				height: (App.getImmersed() + 44) + 'px',
				width: '100%',
				backgroundColor: 'rgba(0,0,0,0)'
			}, [{
				tag: 'font',
				id: 'back',
				text: '\ue582',
				position: {
					left: '0',
					width: '44px',
					top: App.getImmersed() + 'px',
					height: '44px',
					align: 'left'
				},
				textStyles: {
					color: '#fff',
					fontSrc: '_www/lib/mui/fonts/mui.ttf',
					size: '26px'
				}
			}, {
				tag: 'font',
				id: 'backText',
				text: '返回',
				position: {
					left: '24px',
					width: '46px',
					top: App.getImmersed() + 'px',
					height: '44px',
					align: 'left'
				},
				textStyles: {
					color: '#fff',
					size: '16px',
					verticalAlign: 'middle'
				}
			}, {
				tag: 'font',
				id: 'font',
				text: (this.type == 'borrow' ? '租借' : '归还') + '充电宝',
				textStyles: {
					size: '17px',
					color: '#fff'
				},
				position: {
					top: App.getImmersed() + 'px',
					height: '44px'
				},
			}]);
			titleView.addEventListener('click', function(e) {
				var clientX = e.clientX;
				var clientY = e.clientY;
				if(clientY > App.getImmersed()) {
					if(clientX < 70) {
						mui.back();
					}
				}
			})
			//			this.titleView.show();
			plus.webview.currentWebview().append(titleView);
		},
	}
})

window.addEventListener('scanCodeInit', function(event) {
	scanCodeWrapperVm.prevWebViewId = event.detail.webviewId;
	plus.nativeUI.showWaiting();
	scanCodeWrapperVm.scanWrapper.start();
	plus.nativeUI.closeWaiting();
});